package net.zxteam.solutions.java.rs_sync;


import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="init")
final class InitInfo {

	static final JAXBContext jaxbCtx;

	static {

		JAXBContext c = null;
		try { c = JAXBContext.newInstance(InitInfo.class); }
		catch(Exception ex) { c = null; }

		jaxbCtx = c;
	}
	
	public static InitInfo deserialize(String xmlData) throws JAXBException {
		
		Unmarshaller unm = jaxbCtx.createUnmarshaller();
		StringReader sr = new StringReader(xmlData);
		InitInfo ti = (InitInfo)unm.unmarshal(sr);
		return ti;
	}
	
	public static String serialize(InitInfo initInfo) throws JAXBException {
		
		Marshaller m = jaxbCtx.createMarshaller();
		StringWriter sw = new StringWriter();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
		m.setProperty(Marshaller.JAXB_FRAGMENT, true);
		m.marshal(initInfo, sw);
		return sw.toString();
	}
	
	public String getRepositoryUrl() {

		return repositoryRoot + '/' + relativePath;
	}

	@XmlAttribute(name="rev")
	public long revision;

	@XmlAttribute(name="repUrl")
	public String repositoryRoot;

	@XmlAttribute(name="path")
	public String relativePath;
}